local skel = fk.CreateSkill{
  name = "kt_st__daye",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["kt_st__daye"] = "大业",
  [":kt_st__daye"] = "锁定技，我的摸牌阶段改为十张为一组地检索一张【无中生有】，然后我选择一组并获得其中的红色牌，其他角色再票选出另一组，他们可以依次对我使用其中的黑色牌。",

  ["#kt_st__daye_ask1"] = "大业：选择一组并获得其中的红色牌",
  ["#kt_st__daye_ask2"] = "大业：票选出一组（见战报），并可以对 %src 使用其中的黑色牌",
  ["#kt_st__daye_ask3"] = "大业：对 %src 使用一张牌",

  ["$kt_st__daye1"] = "诸勋勒我，尔辈不过撑一肉辇。",
  ["$kt_st__daye2"] = "虚名高记，此下过者败骨有万，龙虎有千。",
  ["$kt_st__daye3"] = "金盖许得你，老匹夫——普六茹。",
  ["$kt_st__daye4"] = "国瘙主害，缘由你些毛芥。",
  ["$kt_st__daye5"] = "好，好！刺王杀驾！",
  ["$kt_st__daye6"] = "来，砍成肉泥，拖出去，杀！",
}

local U = require "packages/utility/utility"


skel:addEffect(fk.EventPhaseStart, {
  mute = true,
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Draw and not data.phase_end
  end,
  on_use = function(self, event, target, player, data)
    data.phase_end = true
    local room = player.room
    local namelist = {}
    local chooselist = {}
    local cards = {}
    local ii = 1
    --亮出牌
    player.room:notifySkillInvoked(player, skel.name)
    player:broadcastSkillInvoke(skel.name,math.random(1,2))
    while 1 do--循环，用break出
      room:sendLog{type = "大业：第 "..ii.." 组",}
      local carda = room:getNCards(10)
      room:moveCards({
        ids = carda,
        toArea = Card.Processing,
        moveReason = fk.ReasonJustMove,
        skillName = skel.name,
        proposer = player.id,
      })
      table.insert(namelist,"第"..ii.."组")
      table.insert(chooselist,carda)
      if table.find(carda,function (card) return Fk:getCardById(card).name =="ex_nihilo"end) then
        room:setCardEmotion(carda[10], "judgegood")
        room:delay(900)
        break
      else
        room:setCardEmotion(carda[10], "judgebad")
        room:delay(900)
        ii = ii + 1
      end
      --
    end
    --选一组
    local choose
    if #chooselist == 1 then
      choose = namelist
    else
      choose = U.askForChooseCardList(room, player, namelist, chooselist, 1, 1, skel.name, "#kt_st__daye_ask1",false,false)
    end
    
    ii = table.indexOf(namelist,choose[1])
    for _, c in ipairs(chooselist[ii]) do
      if Fk:getCardById(c).color == Card.Red then
        table.insertIfNeed(cards,c)
      end
    end
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, skel.name, nil, true, player)
    --自己部分完成
    --room:cleanProcessingArea()
    --其他玩家选一项
    if #namelist == 1 then room:cleanProcessingArea()
      room:cleanProcessingArea()
      return
    end
    table.remove(namelist,ii)
    table.remove(chooselist,ii)
    if #namelist == 1 then ii = 1 else
      player:broadcastSkillInvoke(skel.name,math.random(3,4))
      local otherchoose = room:askToJointChoice(player, {
        players = room:getOtherPlayers(player),
        choices = namelist,
        skill_name = skel.name,
        prompt = "#kt_st__daye_ask2:"..player.id,
        --send_log = true,
      })
      ---从这开始---
      local result = {}
      for _, p in ipairs(room:getOtherPlayers(player)) do
        local index = table.indexOf(namelist,otherchoose[p])
        table.insert(result,index)
      end
      local sum = {}
      local sumb = {}
      for i=1,#namelist do
        sum[i] = 0
        sumb[i] = 0
      end
      for i=1,#result do
        local a = result[i]
        sum[a] = sum[a] + 1
        sumb[a] = sumb[a] + 1
      end
      table.sort(sumb)
      if sumb[#sumb] == sumb[#sumb-1] then
        ii = 0
      else
        ii = table.indexOf(sum,sumb[#sumb])
      end
    end
    ---到这里---
    --在判断哪个选项被选择的次数最多，纯数学内容
    if ii <= 0 then
      room:cleanProcessingArea()
      return
    end
    cards = {}
    for _,c in ipairs(chooselist[ii])do
      if Fk:getCardById(c).color == Card.Black then
        table.insert(cards,c)
      end
    end
    for _,p in ipairs(room:getOtherPlayers(player)) do
      if player.dead then
        room:cleanProcessingArea()
        return
      end
      if #cards > 0 and not p.dead then
        local use = room:askToUseRealCard(p, {
          pattern = cards,
          skill_name = skel.name,
          prompt = "#kt_st__daye_ask3:"..player.id,
          extra_data = {
            bypass_times = true,
            exclusive_targets = {player.id},
            not_passive = true,
            --fix_targets = {player.id},
            expand_pile = cards,
            bypass_distances = true,
          },
          cancelable = true,
          skip = true,
        })
        if use then
          if use.card.trueName == "slash" then
            player:broadcastSkillInvoke(skel.name,math.random(5,6))
          end
          room:useCard(use)
          table.remove(cards,table.indexOf(cards,use.card.id))
        end
      end
    end
    room:cleanProcessingArea()
  end,
})

return skel